package com.itheima.course.design.ui;

import com.itheima.course.design.entity.Course;
import com.itheima.course.design.service.CourseService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class CourseManagementUI extends JFrame {
    private CourseService courseService = new CourseService();
    // 添加courseList成员变量声明
    private JList<Course> courseList;

    public CourseManagementUI() {
        initUI();
    }

    private void initUI() {
        setTitle("课程管理");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new BorderLayout());

        // 创建表格模型和表格
        DefaultListModel<Course> courseListModel = new DefaultListModel<>();
        JList<Course> courseList = new JList<>(courseListModel);
        courseList.setCellRenderer(new CourseListCellRenderer());
        JScrollPane courseScrollPane = new JScrollPane(courseList);
        
        // 将courseList声明为类成员变量以便在refreshCourseList方法中访问
        this.courseList = courseList;
        
        panel.add(courseScrollPane, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());
        
        JButton addButton = new JButton("添加课程");
        JButton refreshButton = new JButton("刷新列表");
        JButton closeButton = new JButton("关闭窗口");
        
        buttonPanel.add(addButton);
        buttonPanel.add(refreshButton);
        buttonPanel.add(closeButton);
        
        panel.add(buttonPanel, BorderLayout.SOUTH);

        add(panel);

        // 添加课程按钮事件
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showAddCourseDialog();
            }
        });

        // 刷新按钮事件
        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                refreshCourseList(courseListModel);
            }
        });

        // 关闭按钮事件
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        // 初始化加载数据
        refreshCourseList(courseListModel);
    }

    // 刷新课程列表
    private void refreshCourseList(DefaultListModel<Course> model) {
        model.clear();
        List<Course> courses = courseService.getAllCourses();
        for (Course course : courses) {
            model.addElement(course);
        }
    }

    // 显示添加课程对话框
    private void showAddCourseDialog() {
        JTextField courseCodeField = new JTextField(20);
        JTextField nameField = new JTextField(20);
        JTextField instructorField = new JTextField(20);
        JTextField maxStudentsField = new JTextField(20);

        JPanel panel = new JPanel(new GridLayout(4, 2));
        panel.add(new JLabel("课程代码:"));
        panel.add(courseCodeField);
        panel.add(new JLabel("课程名称:"));
        panel.add(nameField);
        panel.add(new JLabel("任课教师:"));
        panel.add(instructorField);
        panel.add(new JLabel("最大人数:"));
        panel.add(maxStudentsField);

        int result = JOptionPane.showConfirmDialog(this, panel, "添加新课程", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            String courseCode = courseCodeField.getText();
            String name = nameField.getText();
            String instructor = instructorField.getText();
            
            int maxStudents;
            try {
                maxStudents = Integer.parseInt(maxStudentsField.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "最大人数必须是数字！", "输入错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (courseCode.isEmpty() || name.isEmpty() || instructor.isEmpty()) {
                JOptionPane.showMessageDialog(this, "所有字段都是必填项！", "输入错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            Course course = new Course();
            course.setCourseCode(courseCode);
            course.setName(name);
            course.setInstructor(instructor);
            course.setMaxStudents(maxStudents);

            boolean success = courseService.addCourse(course);
            if (success) {
                JOptionPane.showMessageDialog(this, "课程添加成功！");
                refreshCourseList((DefaultListModel<Course>) courseList.getModel());
            } else {
                JOptionPane.showMessageDialog(this, "课程添加失败，请重试！", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // 课程列表渲染器
    private static class CourseListCellRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof Course) {
                Course course = (Course) value;
                setText(course.getName() + " (课程代码: " + course.getCourseCode() + ", 教师: " + course.getInstructor() + ", 最大人数: " + course.getMaxStudents() + ")");
            }
            return this;
        }
    }
}